// 2025/10/17
// 反转链表二

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* reverseBetween(ListNode* head, int left, int right) {
        ListNode phead(0, head), *cur = &phead, *prev = cur;
        for(int i = 0; i < left; i++)
        {
            prev = cur;
            cur = cur->next;
        }
        ListNode* next, *end = cur;
        for(int i = left; i <= right; i++)
        {
            next = cur->next;
            cur->next = prev->next;
            prev->next = cur;
            cur = next;
        }
        end->next = cur;
        return phead.next;
    }
};